import 'package:path/path.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

import '../../chat/MessageModel.dart';


class DBManager{
  final int _version = 1;//版本号
  final String _databaseName = 'fuck.db';//数据库名称

  ///收藏表
  static const String messageTable = 'message';//表名
  static const String id = 'id';//主键
  static const String messageContent = 'messageContent';//文本内容
  static const String isUser = 'isUser';//是否是用户发送的消息



  static DBManager? _instance;
  static DBManager getInstance() => _instance ??= DBManager();

  static Database? _database;
  Future<Database> get getDatabase async => _database ??= await _initSQl();

  ///初始化数据库
  Future<Database> _initSQl() async{
    sqfliteFfiInit();
    var databaseFactory = databaseFactoryFfi;
    return await databaseFactory.openDatabase(
      inMemoryDatabasePath,
      //打开数据库操作
      options: OpenDatabaseOptions(
        //版本
        version: _version,
        //创建时操作
        onCreate: _onCreate,
      ),
    );
  }

  ///创建表
  Future _onCreate(Database db,int version) async{
    String favoriteSQL =
    '''CREATE TABLE $messageTable(
      $id INTEGER PRIMARY KEY,
      $messageContent TEXT,
      $isUser BOOLEAN
      )
      ''';

    await db.execute(favoriteSQL);
    await db.insert(messageTable,MessageModel(
      id: 0,
      messageContent: '我们已经是好友了 开始聊天吧！',
      isUser: 0,
    ).toJson());
  }
}
